﻿Imports ComponentFactory.Krypton.Toolkit
Imports System.Windows.Forms
Imports System.Windows.Forms.Form

Public Class ComponentColorCleaner
    Implements IDisposable

    Private ctrls As ArrayList
    Private color As Color

    Public Sub add(ByVal c As ControlCollection, ByVal color As Color)
        Me.color = color
        ctrls = New ArrayList
        For Each i As Control In c
            If (TypeOf i Is KryptonTextBox) Then
                AddHandler DirectCast(i, KryptonTextBox).TextChanged, AddressOf textHandler
                ctrls.Add(i)

            ElseIf (TypeOf i Is KryptonMaskedTextBox) Then
                AddHandler DirectCast(i, KryptonMaskedTextBox).TextChanged, AddressOf textHandler
                ctrls.Add(i)

            End If
        Next

    End Sub

    Private Sub textHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        If (TypeOf sender Is KryptonTextBox) Then
            DirectCast(sender, KryptonTextBox).StateCommon.Back.Color1 = Me.color

        ElseIf (TypeOf sender Is KryptonMaskedTextBox) Then
            DirectCast(sender, KryptonMaskedTextBox).StateCommon.Back.Color1 = Me.color

        End If
    End Sub

    Private Sub listHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        DirectCast(sender, KryptonListBox).StateCommon.Back.Color1 = Me.color
    End Sub

    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        ' This object will be cleaned up by the Dispose method.
        ' Therefore, you should call GC.SupressFinalize to
        ' take this object off the finalization queue 
        ' and prevent finalization code for this object
        ' from executing a second time.
        For Each i As Control In ctrls
            If (TypeOf i Is KryptonTextBox) Then
                RemoveHandler DirectCast(i, KryptonTextBox).TextChanged, AddressOf textHandler
                ctrls.Remove(i)

            ElseIf (TypeOf i Is KryptonMaskedTextBox) Then
                RemoveHandler DirectCast(i, KryptonMaskedTextBox).TextChanged, AddressOf textHandler
                ctrls.Remove(i)

            ElseIf (TypeOf i Is KryptonListBox) Then
                RemoveHandler DirectCast(i, KryptonListBox).SelectedIndexChanged, AddressOf listHandler
                ctrls.Remove(i)

            End If
        Next
        GC.SuppressFinalize(Me)
    End Sub

End Class